package com.example.replacebooks.service;

import cn.hutool.core.date.DateUtil;
import com.example.replacebooks.common.JwtTokenUtils;
import com.example.replacebooks.entity.Bookinfo;
import com.example.replacebooks.entity.Changebook;
import com.example.replacebooks.entity.Params;
import com.example.replacebooks.entity.User;
import com.example.replacebooks.mapper.BookinfoMapper;
import com.example.replacebooks.mapper.ChangebookMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ChangbookService {
    @Resource
    private ChangebookMapper changebookMapper;

    // 正常添加
    public void add(Changebook changebook) {
        // 进行重复性判断，同一id的书籍修改其内容
        Changebook changebookid = changebookMapper.findByChangebookid(changebook.getChangebookid());
        if (changebookid != null){
            // 该id已存在
            changebookMapper.updateBybookid(changebook);
        }else{
            User currentUser = JwtTokenUtils.getCurrentUser();
            Integer userid = currentUser.getUserid();
            changebook.setUserid(userid);
            changebookMapper.insertSelective(changebook);
        }
    }

    //获取地址后的添加
    public void addAddress(Changebook changebook) {
        changebookMapper.updateBybookid(changebook);
        System.out.println(changebook);
        if (changebook.getAddress() != null && changebook.getChangeaddress() != null){
            changebook.setChangestatu("已完成");
            changebook.setChangetime(DateUtil.now());
            changebookMapper.updateBybookid(changebook);
        }
    }

    // 通过审核
    public PageInfo<Changebook> findBySearch(Params params) {
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Changebook> list = changebookMapper.findBySearch(params);
        return PageInfo.of(list);
    }

    //未通过审核
    public PageInfo<Changebook> findByStatuSearch(Params params) {
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Changebook> list = changebookMapper.findByStatuSearch(params);
        return PageInfo.of(list);
    }

    public void delete(Integer changebookid) {
        changebookMapper.deleteByPrimaryKey(changebookid);
    }

    public Changebook findByBookid(Integer changebookid) {
        Changebook list = changebookMapper.findByChangebookid(changebookid);
        return list;
    }

    //通过审核
    public void savestatu(Changebook changebook) {
        // 更新审核状态和审核时间
        changebook.setChangestatu("已通过");
        changebook.setTime(DateUtil.now());
        changebookMapper.updateBybookid(changebook);
    }

    //完成置换书籍

}
